﻿using SelfMadeMillionaire.Core;
using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Enums;
using SelfMadeMillionaire.Core.Repositories;
using SelfMadeMillionaire.Core.Services;
using SelfMadeMillionaire.Core.Specifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelfMadeMillionaire.BLL.Services
{
	public class PublicationService : IPublicationService
	{
		private readonly IPublicationRepository _publicationRepository;
		private readonly IEmailMessageRepository _emailMessageRepository;
		private readonly ISchedulerService _schedulerService;

		public PublicationService(
			IPublicationRepository publicationRepository, 
			IEmailMessageRepository emailMessageRepository,
			ISchedulerService schedulerService)
		{
			_publicationRepository = publicationRepository;
			_emailMessageRepository = emailMessageRepository;
			_schedulerService = schedulerService;
		}

		public Publication Add(Publication publication)
		{
			publication.CreatedDate = DateTime.Now;
			publication.Status = 1;
			return _publicationRepository.Add(publication);
		}

		public Publication Update(Publication publication)
		{
			return _publicationRepository.Update(publication);
		}

		public Publication Get(int id)
		{
			return _publicationRepository.Get(id);
		}

		public IEnumerable<Publication> GetList(ISearchSpecification<Publication> specification)
		{
			return _publicationRepository.GetList(specification);
		}

		public void Remove(int id)
		{
			var pub = Get(id);
			pub.Status = (byte)StatusType.Deleted;
			Update(pub);
		}

		public void Send(int id, string emailTemplate)
		{
			var pub = Get(id);

			//string body = pub.HtmlContent;

			//if (!string.IsNullOrWhiteSpace(pub.PdfFileName))
			//	body = FileLink.GetAbsoluteLinkOnFile(pub.PdfFileName);

			string body = emailTemplate
				.Replace("[issueTypeName]", pub.Title)
				.Replace("[currentYear]", DateTime.Now.Year.ToString());

			var message = new EmailMessage
			{
				Title = pub.Title,
				Body = body,
				ProductType = pub.ProductType,
				MinAccessLevel = pub.MinAccessLevel
			};


            // для MMM новостей нет ограничений, всем отсылаем сразу же
            if (message.ProductType == (byte)ProductType.MaggiesMoneyMountain)
            {
                message.FreeSendDate = DateTime.Now.Date;
                message.SilverSendDate = DateTime.Now.Date;
                message.GoldSendDate = DateTime.Now.Date;
            }
            else
            {
                SetSendFreeDate(pub, 14, message);
                SetSendSilverDate(pub, 7, message);
                SetSendGoldDate(pub, 1, message);
                SetSendPlatinumDate(pub, 0, message);
            }


			pub.EmailMessage = _emailMessageRepository.Add(message);

			_publicationRepository.Commit();
		}

        void SetSendFreeDate(Publication nl, int daysShift, EmailMessage em)
        {
            if (nl.MinAccessLevel >= (byte)SubscriptionType.Free)
            {
                em.FreeSendDate = DateTime.Now.Date.AddDays(daysShift);
            }
        }

        void SetSendSilverDate(Publication nl, int daysShift, EmailMessage em)
        {
            if (nl.MinAccessLevel >= (byte)SubscriptionType.Silver)
            {
                em.SilverSendDate = DateTime.Now.Date.AddDays(daysShift);
            }
        }

        void SetSendGoldDate(Publication nl, int daysShift, EmailMessage em)
        {
            if (nl.MinAccessLevel >= (byte)SubscriptionType.Gold)
            {
                em.GoldSendDate = DateTime.Now.Date.AddDays(daysShift);
            }
        }

        void SetSendPlatinumDate(Publication nl, int daysShift, EmailMessage em)
        {
            if (nl.MinAccessLevel >= (byte)SubscriptionType.Platinum)
            {
                em.PlatinumSendDate = DateTime.Now.Date;
            }
        }

        public void Hide(int id)
        {
            Publication pub = Get(id);
            pub.IsShowOnHomePage = false;
            Update(pub);
        }

        public void Show(int id)
        {
            Publication pub = Get(id);
            pub.IsShowOnHomePage = true;
            Update(pub);
        }
	}
}
